package send;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class WechatSend {
    public static void WechatSend(String accessToken,String message){
        String url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+accessToken;
        try {
            URL url1 = new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type","application/json");

            String jsonInputString = "{\"touser\":\"USER_ID\"," +
                    "\"msgtype\":\"text\"," +
                    "\"agentid\":AGENTID," +
                    "\"text\":{\"content\":\"" + message + "\"}," +
                    "\"safe\":\"0\"}";
urlConnection.setDoOutput(true);
urlConnection.getOutputStream().write(jsonInputString.getBytes("UTF-8"));
urlConnection.getOutputStream().flush();
urlConnection.getOutputStream().close();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line=null;
StringBuilder builder = new StringBuilder();
while ((line=bufferedReader.readLine())!=null){
    builder.append(line);
}
bufferedReader.close();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
